#include <irrlicht.h>
#include "MyEventReceiver.h"
#include "AdditRes.h"

#include <windows.h>
#include <WinUser.h>
#include <scrnsave.h>
#include <commctrl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>
using namespace std;

#ifdef UNICODE
#pragma comment(lib, "scrnsavw.lib")
#else
#pragma comment(lib, "scrnsave.lib")
#endif
#pragma comment(lib, "comctl32.lib")

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

IrrlichtDevice *device = nullptr;
MyEventReceiver receiver;
IVideoDriver* driver = nullptr;
ISceneManager* smgr = nullptr;
IAnimatedMesh* mesh = nullptr;
IAnimatedMeshSceneNode* node = nullptr;

#define DEFVEL  5  

LONG lSpeed = DEFVEL;

void writeToFile(char* wm_case)
{
  ofstream myfile;
  myfile.open("example.txt");
  myfile << __TIMESTAMP__;
  myfile << wm_case;
  myfile.close();
}

bool initDevice()
{
  device = createDevice(video::EDT_SOFTWARE, dimension2d<u32>(640, 480), 32, true, false, true, &receiver);

  if (!device)
    device->closeDevice();

  device->setWindowCaption(L"Screen Saver");
  device->getCursorControl()->setVisible(false);

  driver = device->getVideoDriver();
  smgr = device->getSceneManager();

  mesh = smgr->getMesh("../media/faerie.md2");

  if (!mesh)
  {
    device->closeDevice();
  }
  node = smgr->addAnimatedMeshSceneNode(mesh);

  if (node)
  {
    node->setMaterialFlag(EMF_LIGHTING, false);
    node->setMD2Animation(scene::EMAT_STAND);
    node->setMaterialTexture(0, driver->getTexture("../media/faerie2.bmp"));
  }
  smgr->addCameraSceneNode(0, vector3df(0, 30, 40), vector3df(0, 5, 0));

  return 0;
}

bool drawAnimation()
{
  while (device->run())
  {
    driver->beginScene(true, true, SColor(255, 100, 101, 140));
    smgr->drawAll();
    if (receiver.MouseState.LeftButtonDown || receiver.MouseState.RightButtonDown || receiver.KeyState.KeyIsDown)
    {
      device->closeDevice();
    }
    driver->endScene();
    //if (receiver.MouseState.MouseMoved)
    //  break;
  }
  return 0;
}


void dropAnimation()
{
  if (device)
    device->drop(); 
}


BOOL WINAPI ScreenSaverConfigureDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
  writeToFile("ConfDialog");
  switch (message)
  {
  case WM_COMMAND:
    switch (LOWORD(wParam))
    {
    case IDOK:
      EndDialog(hDlg, LOWORD(wParam) == IDOK);

      return TRUE;

    case IDCANCEL:
      EndDialog(hDlg, LOWORD(wParam) == IDOK);

      return TRUE;
    }
  }
  return FALSE;
}

LRESULT WINAPI ScreenSaverProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  static HDC          hdc;      // device-context handle  
  static RECT         rc;       // RECT structure  
  static UINT         uTimer;   // timer identifier 

  switch (message)
  {
  case WM_CREATE:
  {
                  writeToFile("\nCREATE"); 
                  initDevice();
                  //drawAnimation();
  }
    break;

  case WM_SIZE:
    drawAnimation();
    break;

  case WM_DESTROY:
  {
                   writeToFile("\nDESTROY");
                   DestroyWindow(hwnd);
                   dropAnimation();
                   PostQuitMessage(0);
                   return 0;
  }
    break;
  
  }

  return DefScreenSaverProc(hwnd, message, wParam, lParam);
}


BOOL WINAPI RegisterDialogClasses(HANDLE hInst)
{
  return TRUE;
}



